/*
 clsquare - closed loop simulation system
 Copyright (c) 2004, Neuroinformatics Group, Prof. Dr. Martin Riedmiller,
 University of Osnabrueck
 
 Author: Sascha Lange
 
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
 met:
 
   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the University of Osnabrueck nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 DAMAGE. 
 */


#include "registry.h"
#include <iostream>
#include <cstring>
#include <cstdlib>

#define ERROUT_REG( __x__ ) std::cerr << "#ERROR: " << __FILE__ << " in Line: " << __LINE__ << " in " << __PRETTY_FUNCTION__ << __x__ << "\n"

AbstractRegistry::AbstractRegistry()
  : size(0)
{}

AbstractRegistry::~AbstractRegistry()
{
  for (int i=0; i < size; i++) {
    delete entries[i].factory;
  }
}

void AbstractRegistry::add(const char* name, const char* desc, 
			   Factory *factory) {
  if (size == MAX_ENTRIES) {
    ERROUT_REG("Too many entries : " << MAX_ENTRIES << " exiting!");
    exit(1);
  }
  entries[size].name = name;
  entries[size].desc = desc;
  entries[size++].factory = factory;
}

AbstractRegistry::Factory* AbstractRegistry::lookup(const char* name) const
{
  for (int i=0; i < size; i++) {
    if (strcmp(name, entries[i].name) == 0) {
      return entries[i].factory;
    }
  }
  return 0;  
}

int AbstractRegistry::listEntries(char* buf, int buf_size) const 
{
  buf[0] = '\0';
  int available = buf_size - 1;
  for (int i=0; i < size; i++) {
    strncat(buf, entries[i].name, available);
    available -= strlen(entries[i].name);
    strncat(buf, "\n\t", available);
    available -= 2;
    strncat(buf, entries[i].desc, available);
    available -= strlen(entries[i].desc);
    strncat(buf, "\n", available);
    available -= 1;
    if (available < 0) {
      return 1;
    }
  }
  buf[buf_size-1] = '\0'; // safety first
  return 0;
}
      

Plant* PlantFactory::create(const char* name) const
{
  Factory* factory = lookup(name);
  if (! factory) {
    return 0;
  }
  else {
    return (Plant*) factory->create();
  }
}


PlantFactory* PlantFactory::getThePlantFactory() 
{
  if (! the_plantFactory) {
    the_plantFactory = new PlantFactory();
  }
  return the_plantFactory;
}

Controller* ControllerFactory::create(const char* name) const
{
  Factory* factory = lookup(name);
  if (! factory) {
    return 0;
  }
  else {
    return (Controller*) factory->create();
  }
}


ControllerFactory* ControllerFactory::getTheControllerFactory() 
{
  if (! the_controllerFactory) {
    the_controllerFactory = new ControllerFactory();
  }
  return the_controllerFactory;
}

Graphic* GraphicFactory::create(const char* name) const
{
  Factory* factory = lookup(name);
  if (! factory) {
    return 0;
  }
  else {
    return (Graphic*) factory->create();
  }
}


GraphicFactory* GraphicFactory::getTheGraphicFactory() 
{
  if (! the_graphicFactory) {
    the_graphicFactory = new GraphicFactory();
  }
  return the_graphicFactory;
}


PlantFactory* PlantFactory::the_plantFactory = 0; 
ControllerFactory* ControllerFactory::the_controllerFactory = 0; 
GraphicFactory* GraphicFactory::the_graphicFactory = 0; 
